package com.happyscan138.makefun;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import com.happyscan138.makefun.BaseAdapter.my_join;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.myJoinAdapter;
import com.happyscan138.makefun.base.LoadListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/8.
 */

public class myJoin extends AppCompatActivity implements View.OnClickListener, LoadListView.IloadListener {
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private String access_key;
    private LoadListView listView_myJoin;
    private List<my_join> listdata;
    private Button back;
    private myJoinAdapter adapter;
    private my_join data;
    private Intent intent;
    private Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_join);
        client=new AsyncHttpClient();
        params=new RequestParams();
        preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        initView();
        getAdapterView();
    }
    public void initView(){
        listView_myJoin=(LoadListView)findViewById(R.id.my_join_listview);
        listView_myJoin.setInterface(this);
        back=(Button)findViewById(R.id.MyJoin_back);
        back.setOnClickListener(this);
    }
    public View getAdapterView(){
        View view=null;
        params.put("access_key",access_key);
        params.put("page_count","10");
        params.put("sel_id_index","0");
        RequestHandle handle=client.post(Urls.MyJoin,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                log.e("jsonStr",jsonStr);
                listdata=new ArrayList<>();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject objectData=jsonArray.getJSONObject(i);
                        log.e("objectData",objectData.toString());
                        data =new my_join();
                        data.head= base+objectData.getString("img_path");
//                        log.e("icon",data.icon.toString());
//                        data.name=objectData.getString("nick_name");
                        data.time=objectData.getString("create_date");
//                        data.text=objectData.getString("content");
                        data.image= base+objectData.getString("resource");
                        data.comment=objectData.getString("content");
                        data.problem_id= objectData.getString("problem_id");
                        listdata.add(data);

                    }
                    log.e("listData",listdata.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter=new myJoinAdapter(myJoin.this,listdata);
                listView_myJoin.setAdapter(adapter);
                listView_myJoin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        intent = new Intent();
                        data = listdata.get(position);
                        intent.putExtra("problem_id", data.problem_id);
                        intent.setClass(myJoin.this, questionDetailed.class);
                        startActivity(intent);
                    }
                });

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                log.e("responseString",responseString);
            }
        });


        return view;
    }

    @Override
    public void onClick(View v) {
        switch ((v.getId())){
            case R.id.MyJoin_back:
                finish();
                break;
        }
    }

    @Override
    public void onLoad() {
        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                params.put("access_key",access_key);
                params.put("page_count","10");
                params.put("sel_id_index","0");
                RequestHandle handle=client.post(Urls.MyJoin,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            JSONArray jsonArray=jsonObject.getJSONArray("data");
                            for (int i=0;i<jsonArray.length();i++){
                                JSONObject objectData=jsonArray.getJSONObject(i);
                                log.e("objectData",objectData.toString());
                                data =new my_join();
                                data.head= base+objectData.getString("img_path");
//                        log.e("icon",data.icon.toString());
//                        data.name=objectData.getString("nick_name");
                                data.time=objectData.getString("create_date");
//                        data.text=objectData.getString("content");
                                data.image= base+objectData.getString("resource");
                                data.comment=objectData.getString("content");
                                data.problem_id= objectData.getString("problem_id");
                                listdata.add(data);

                            }
                            log.e("listData",listdata.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
                listView_myJoin.loadComplete();
            }
        },2000);
    }
}
